package com.dd.utils;

import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

public class Toast {
    public static void show(String message, Stage owner, int durationMs) {
        Stage toastStage = new Stage();
        toastStage.initModality(Modality.NONE);
        toastStage.initStyle(StageStyle.TRANSPARENT);
        toastStage.initOwner(owner);

        Label label = new Label(message);
        label.setStyle("-fx-background-color: rgba(0, 0, 0, 0.7); -fx-text-fill: white; -fx-padding: 10px; -fx-border-radius: 5px; -fx-background-radius: 5px;");

        Scene scene = new Scene(label);
        scene.setFill(javafx.scene.paint.Color.TRANSPARENT);
        toastStage.setScene(scene);

        // 定位到屏幕底部居中
        toastStage.setX(owner.getX() + owner.getWidth() / 2 - 100);
        toastStage.setY(owner.getY() + owner.getHeight() - 100);

        toastStage.show();

        // 自动关闭
        javafx.animation.PauseTransition delay = new javafx.animation.PauseTransition(Duration.millis(durationMs));
        delay.setOnFinished(e -> toastStage.close());
        delay.play();
    }
}